package com.hzlx.service.impl;/*
 *@author liu
 *2024/07/2024/7/25
 *@version
 */

import com.hzlx.common.ResponseBean;
import com.hzlx.entity.AccountFlow;
import com.hzlx.entity.CustomerInfo;
import com.hzlx.entity.WithdrawalRecords;
import com.hzlx.mapper.AccountFlowMapper;
import com.hzlx.mapper.CustomerInfoMapper;
import com.hzlx.mapper.WithdrawalRecordsMapper;
import com.hzlx.service.CustomerInfoService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author UP
 * @Date 2024/7/25 10:03
 * @Version 1.0
 **/
@Service
public class CustomerInfoServiceImpl implements CustomerInfoService {
    @Autowired
    private CustomerInfoMapper customerInfoMapper;
    @Value("${session.key}")
    private String sessionKey;
    @Autowired
    private WithdrawalRecordsMapper withdrawalRecordsMapper;
    @Autowired
    private AccountFlowMapper accountFlowMapper;

    @Override
    public ResponseBean login(String userName, String password, HttpServletRequest request) {
        if (StringUtils.isEmptyOrWhitespaceOnly(userName) || StringUtils.isEmptyOrWhitespaceOnly(password)) {
            return new ResponseBean(10002, "账号密码不能为空", null);
        }
        CustomerInfo customerInfo = customerInfoMapper.getAccountByUserNameAndPassword(userName, password);
        if (customerInfo == null) {
            return new ResponseBean(10003, "用户名或密码错误", null);
        }
        //把登录信息存Session
        request.getSession().setAttribute(sessionKey, customerInfo);
        return new ResponseBean(0, "success", customerInfo);
    }

    @Override
    public ResponseBean getAll() {
        List<CustomerInfo> customerInfoList = customerInfoMapper.getAll();
        if (customerInfoList != null) {
            return new ResponseBean(0, "success", customerInfoList);
        }
        return new ResponseBean(1000, "失败", null);
    }

    @Override
    public ResponseBean add(Integer accountId, Integer beforeBalance, Integer money, Integer cardId) {
        //1.修改客户余额
        //2.新增客户提现记录表
        WithdrawalRecords withdrawalRecords = new WithdrawalRecords();
        //设置提现流水号
        withdrawalRecords.setFlowNo(String.valueOf(System.currentTimeMillis()));
        //设置客户id
        withdrawalRecords.setAccountId(accountId);
        //设置银行卡id
        withdrawalRecords.setBankCardId(cardId);
        //设置提现前金额
        withdrawalRecords.setAmountBeforeWithdrawal(BigDecimal.valueOf(beforeBalance));
        //设置提现金额
        withdrawalRecords.setAmount(BigDecimal.valueOf(money));
        //设置提现后金额
        withdrawalRecords.setAmountAfterWithdrawal(BigDecimal.valueOf((beforeBalance - money)));
        withdrawalRecordsMapper.saveWithdrawalRecords(withdrawalRecords);

        return new ResponseBean(0, "success", null);
    }
}
